$(function(){
    init();
})

function init(){
    var begin = $("#begin").val();
    var end = $("#end").val();
    if( isIntNum(begin)==isIntNum(end) && isIntNum(begin)){
        ballPeriodCount(begin,end);
        getRecode(begin,end);
    }else{
        alert("请输入正确的期数");
    }
}
//获取单个数字统计数据
function ballPeriodCount(begin,end) {
    $.ajax({
        type: "GET",
        url: "./countPeriod",
        data: {
            begin:begin,
            end:end
        },
        dataType: "json",
        success: function(data){
            if(null != data && data != ''){
                var redName = new Array();
                var redData = new Array();
                var blueName = new Array();
                var blueData = new Array();
                for(var key in data){
                    if(key.indexOf("red") > -1){
                        var nameKey = key.substring(4);
                        redName.push(nameKey);
                        redData.push(data[key]);
                    }
                    if(key.indexOf("blue") > -1){
                        var nameKey = key.substring(5);
                        blueName.push(nameKey);
                        blueData.push(data[key]);
                    }
                }
                initBar(redName,redData,"红球分布","chart1","#CD3333");
                initBar(blueName,blueData,"蓝球分布","chart2","#5CACEE");

            }
        }
    });

}


//获取数字分布数据
function getRecode(begin,end) {
    $.ajax({
        type: "get",
        url: "./getRecode",
        data: {
            begin:begin,
            end:end
        },
        dataType: "json",
        success: function(data){
            if(null != data && data != ''){

                var redTotal = data.redTotal;
                var blueTotal = data.blueTotal;
                var allTotal = data.allTotal;
                var redOddEven = data.redOddEven;
                var blueOddEven = data.blueOddEven;

                chart3left(redTotal);
                chart3right(blueTotal);
                chart4(allTotal);
                chart5left(redOddEven);
                chart5rigth(blueOddEven);

            }
        }
    });
}
//chart3-left
function chart3left(redTotal){
    var name = new Array();
    var data = new Array();
    if(redTotal!=null){
        for(var i in redTotal){
            var temp = redTotal[i];
            name.push(temp.red_total);
            data.push(temp.count);
        }
        initBarNoLine(name,data,"红球总和次数统计","chart3-left","#CD3333")
    }
}

//chart3-right
function chart3right(blueTotal){
    var name = new Array();
    var data = new Array();
    if(blueTotal!=null){
        for(var i in blueTotal){
            var temp = blueTotal[i];
            name.push(temp.blue_total);
            data.push(temp.count);
        }
        initBarNoLine(name,data,"蓝球总和次数统计","chart3-right","#5CACEE")
    }
}

//chart4 allTotal
function chart4(allTotal){
    var name = new Array();
    var data = new Array();
    if(allTotal!=null){
        for(var i in allTotal){
            var temp = allTotal[i];
            name.push(temp.all_total);
            data.push(temp.count);
        }
        initBarNoLine(name,data,"红蓝总和次数统计","chart4","#0099CC")
    }
}

//chart5 red
function chart5left(redOddEven){
    var name = new Array();
    var data = new Array();
    if(redOddEven!=null){
        for(var i in redOddEven){
            var temp = redOddEven[i];
            name.push(temp.odd_even_r);
            data.push(temp.count);
        }
        initBarNoLine(name,data,"红球奇偶比","chart5-left","#CD3333")
    }
}
//chart5 red
function chart5rigth(blueOddEven){
    var name = new Array();
    var data = new Array();
    if(blueOddEven!=null){
        for(var i in blueOddEven){
            var temp = blueOddEven[i];
            name.push(temp.odd_even_b);
            data.push(temp.count);
        }
        initBarNoLine(name,data,"蓝球奇偶比","chart5-right","#5CACEE")
    }
}